package com.ydd.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.context.BaseContext;
import com.ydd.context.BaseStringContext;
import com.ydd.dto.*;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddCate;
import com.ydd.exception.BaseException;
import com.ydd.mapper.YddArticleMapper;
import com.ydd.mapper.YddCateMapper;
import com.ydd.mapper.YddUserDataMapper;
import com.ydd.mapper.YddUserMapper;
import com.ydd.result.PageResult;
import com.ydd.service.YddArticleService;
import com.ydd.utils.PageResultUtil;
import com.ydd.vo.ArticleVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:26
 */
@Service
public class YddArticleServiceImpl implements YddArticleService {
    @Autowired
    private YddArticleMapper yddArticleMapper;

    @Autowired
    private YddCateMapper yddCateMapper;

    @Autowired
    private YddUserDataMapper yddUserDataMapper;

    /**
     * 文章修改
     *
     * @param articleDTO
     */
    @Override
    public void update(ArticleDTO articleDTO) {
        String user_id = BaseStringContext.getCurrentId();
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(articleDTO,yddArticle);
        //设置用户id 填充数据
        yddArticle.setUserId(user_id);
        yddArticle.setDefaultData(articleDTO.getDefault_data());
        yddArticle.setCreateDate(articleDTO.getCreate_date());
        yddArticle.setUpdateDate(articleDTO.getUpdate_date());



/*        //先修改文章标签为空(清空标签)
        yddArticleMapper.delTags(articleDTO.getId());*/
        //先修改文章
        yddArticleMapper.update(yddArticle);

    }

    /**
     * GET文章详细
     * 通过文章id查询文章详细
     *
     * @param id
     * @return
     */
    @Override
    public ArticleVO find(Long id) {

        ArticleVO articleVO =  yddArticleMapper.find(id);
        return articleVO;
    }

    /**
     * 新增文章
     *
     * @param articleDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ArticleDTO articleDTO) {
        //获取当前线程id
        String user_id = BaseStringContext.getCurrentId();
        LocalDate default_data = LocalDate.now();
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(articleDTO,yddArticle);
        //获取当前时间毫秒值
        long now = System.currentTimeMillis();
        //填充
        yddArticle.setCreateDate(now);
        yddArticle.setUpdateDate(now);
        yddArticle.setUserId(user_id);
        yddArticle.setStatus(0);
        //设置为默认
        yddArticle.setDefaultData("1");
        //yddArticleMapper.insert(yddArticle);
        yddArticleMapper.add(yddArticle);
    }

    /**
     * 通过id删除文章
     *
     * @param id
     */
    @Override
    public void del(String id) {
        yddArticleMapper.del(id);
    }

    /**
     * 文章列表分页
     *
     * @param articlePageDTO
     * @return
     */
    @Override
    public PageResult list(ArticlePageDTO articlePageDTO) {

        //设置分页插件
        PageHelper.startPage(articlePageDTO.getPage(),articlePageDTO.getLimit());

        //查询符合条件的表
        List<ArticleVO> pageYddArticle = yddArticleMapper.list(articlePageDTO);

        Page<ArticleVO> list = (Page<ArticleVO>)pageYddArticle;
        //查询符合条件的数据有多少条
        //  Integer count = yddArticleMapper.getCount(articlePageDTO);
        //根据分类id去分类表查询名字
        for (ArticleVO articleVO : list) {
            YddCate yddCate = yddCateMapper.findByid(articleVO.getCateid());

            articleVO.setCatename(yddCate.getCatename());
        }
        //通过分类id查询分类名字
        //封装数据返回
        Integer totalPages = list.size() / list.getPageSize();
        //总记录数 总页数 每页数量 当前页码 列表数据
        return new PageResult(list.getTotal(),totalPages+1,list.getPageSize(),list.getPageNum(),pageYddArticle);


    }

    @Override
    public PageResult myArticle(ArticleAllPageQueryDTO articleAllPageQueryDTO) {
        Long yddUserId = BaseContext.getCurrentId();
        PageHelper.startPage(articleAllPageQueryDTO.getPage(),articleAllPageQueryDTO.getLimit());
        List<YddArticle> yddArticleList = yddArticleMapper.findArticleByUserId(yddUserId);
        Page<YddArticle> page = (Page<YddArticle>) yddArticleList;
        return PageResultUtil.getPageResult(page);
    }

    /**
     * 查询个人收藏/点赞文章
     * @param articleCollectOperateDTO
     * @return
     */
    @Override
    public PageResult userDataList(ArticleCollectPageQueryDTO articleCollectOperateDTO) {
        Long yddUserId = BaseContext.getCurrentId();
        List<Integer> articleIds = yddUserDataMapper.findArticleIdsByUserIdAndType(yddUserId,articleCollectOperateDTO.getType());
        if(articleIds==null||articleIds.size()==0){
            throw new BaseException("暂无数据");
        }
        PageHelper.startPage(articleCollectOperateDTO.getPage(), articleCollectOperateDTO.getLimit());
        List<YddArticle> yddArticleList = yddArticleMapper.findArticleByArticleIds(articleIds);
        Page<YddArticle> page = (Page<YddArticle>) yddArticleList;
        return PageResultUtil.getPageResult(page);
    }

    /**
     * 用户点赞/取消点赞和收藏/取消收藏
     * @param articleCollectOperateDTO
     */
    @Override
    public void userDataHandle(ArticleCollectOperateDTO articleCollectOperateDTO) {
        Long yddUserId = 136l;
        if(articleCollectOperateDTO.getAction().equals("add")){
            int dataCount = yddUserDataMapper.userDataHandleCount(articleCollectOperateDTO,yddUserId);
            if (dataCount==0){
                yddUserDataMapper.insertData(articleCollectOperateDTO,yddUserId);
            }else{
                throw new BaseException("此文章已点赞或收藏");
            }
        }else{
            yddUserDataMapper.deleteData(articleCollectOperateDTO,yddUserId);
        }
    }

    /**
     * 用户端添加文章
     * @param articleDTO
     */
    @Override
    public void addArticle(ArticleDTO articleDTO) {
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(articleDTO,yddArticle);
        String yydUserId = BaseStringContext.getCurrentId();
        yddArticle.setUserId(yydUserId);
        yddArticle.setCreateDate(System.currentTimeMillis());
        yddArticleMapper.insertArticle(yddArticle);
    }

}

